/*
 * ErrorDialog.java
 *
 * Created on 24. Juli 2005, 23:27
 */

package de.gidoo.owl;

import java.util.ResourceBundle;
import javax.swing.Icon;
import javax.swing.ImageIcon;
import javax.swing.JViewport;
import javax.swing.UIManager;

/**
 * Shows an error produced by an exception.
 * @author Thomas Krause
 */
public class ErrorDialog extends javax.swing.JDialog {
  
  private ResourceBundle i18n;
  
  /** Creates new form ErrorDialog */
  public ErrorDialog(java.awt.Frame parent, boolean modal, ResourceBundle i18n) {
    super(parent, modal);
    this.i18n = i18n;
    initComponents();
    
  }
  
  /** This method is called from within the constructor to
   * initialize the form.
   * WARNING: Do NOT modify this code. The content of this method is
   * always regenerated by the Form Editor.
   */
  // <editor-fold defaultstate="collapsed" desc=" Generated Code ">//GEN-BEGIN:initComponents
  private void initComponents() {
    lblIcon = new javax.swing.JLabel();
    lblIcon.setIcon(UIManager.getIcon("OptionPane.errorIcon"));
    btDetails = new javax.swing.JToggleButton();
    scrollPaneMessage = new javax.swing.JScrollPane();
    txtMessage = new javax.swing.JTextArea();
    btOk = new javax.swing.JButton();
    scrollPaneDetail = new javax.swing.JScrollPane();
    txtDetail = new javax.swing.JTextArea();
    lblBugReport = new javax.swing.JLabel();

    getContentPane().setLayout(null);

    setTitle(i18n.getString("errorTitle"));
    setResizable(false);
    lblIcon.setFont(new java.awt.Font("Dialog", 1, 18));
    lblIcon.setText(i18n.getString("errorTitle"));
    getContentPane().add(lblIcon);
    lblIcon.setBounds(5, 5, 410, 32);

    btDetails.setFont(new java.awt.Font("Dialog", 0, 12));
    btDetails.setMnemonic(i18n.getString("btDetailsChar").charAt(0));
    btDetails.setText(i18n.getString("btDetails"));
    btDetails.addActionListener(new java.awt.event.ActionListener() {
      public void actionPerformed(java.awt.event.ActionEvent evt) {
        btDetailsActionPerformed(evt);
      }
    });

    getContentPane().add(btDetails);
    btDetails.setBounds(5, 185, 100, 25);

    scrollPaneMessage.setBorder(new javax.swing.border.EtchedBorder());
    txtMessage.setBackground(javax.swing.UIManager.getDefaults().getColor("Panel.background"));
    txtMessage.setEditable(false);
    txtMessage.setLineWrap(true);
    txtMessage.setWrapStyleWord(true);
    txtMessage.setBorder(null);
    scrollPaneMessage.setViewportView(txtMessage);

    getContentPane().add(scrollPaneMessage);
    scrollPaneMessage.setBounds(5, 42, 410, 108);

    btOk.setFont(new java.awt.Font("Dialog", 0, 12));
    btOk.setMnemonic(i18n.getString("btOkChar").charAt(0));
    btOk.setText(i18n.getString("btOk"));
    btOk.addActionListener(new java.awt.event.ActionListener() {
      public void actionPerformed(java.awt.event.ActionEvent evt) {
        btOkActionPerformed(evt);
      }
    });

    getContentPane().add(btOk);
    btOk.setBounds(4, 155, 100, 25);

    scrollPaneDetail.setBorder(new javax.swing.border.EtchedBorder());
    scrollPaneDetail.setMinimumSize(new java.awt.Dimension(0, 0));
    txtDetail.setBackground(javax.swing.UIManager.getDefaults().getColor("Panel.background"));
    txtDetail.setEditable(false);
    txtDetail.setFont(new java.awt.Font("DialogInput", 0, 12));
    txtDetail.setLineWrap(true);
    txtDetail.setBorder(null);
    scrollPaneDetail.setViewportView(txtDetail);

    getContentPane().add(scrollPaneDetail);
    scrollPaneDetail.setBounds(5, 220, 410, 190);

    lblBugReport.setFont(new java.awt.Font("Dialog", 0, 12));
    lblBugReport.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
    lblBugReport.setText(i18n.getString("lblSubmitBug"));
    lblBugReport.setVerticalAlignment(javax.swing.SwingConstants.BOTTOM);
    getContentPane().add(lblBugReport);
    lblBugReport.setBounds(110, 155, 300, 55);

    java.awt.Dimension screenSize = java.awt.Toolkit.getDefaultToolkit().getScreenSize();
    setBounds((screenSize.width-430)/2, (screenSize.height-243)/2, 430, 243);
  }
  // </editor-fold>//GEN-END:initComponents

  private void btDetailsActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btDetailsActionPerformed
    
    if(btDetails.isSelected())
    {
      //scrollPaneDetail.setSize(scrollPaneDetail.getWidth(), 200);
      setSize(getWidth(), getHeight() + 200);
      btDetails.setText(i18n.getString("btDetailsSelected"));
    }
    else
    {
      //scrollPaneDetail.setSize(scrollPaneDetail.getWidth(), 0);
      setSize(getWidth(), getHeight() - 200);
      btDetails.setText(i18n.getString("btDetails"));
    }
    // this is important e.g. at WinXP
    validate();
  }//GEN-LAST:event_btDetailsActionPerformed

  private void btOkActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btOkActionPerformed
    this.setVisible(false);
  }//GEN-LAST:event_btOkActionPerformed
  
  
  // Variables declaration - do not modify//GEN-BEGIN:variables
  private javax.swing.JToggleButton btDetails;
  private javax.swing.JButton btOk;
  private javax.swing.JLabel lblBugReport;
  private javax.swing.JLabel lblIcon;
  private javax.swing.JScrollPane scrollPaneDetail;
  private javax.swing.JScrollPane scrollPaneMessage;
  private javax.swing.JTextArea txtDetail;
  private javax.swing.JTextArea txtMessage;
  // End of variables declaration//GEN-END:variables
  
  /**
   * Pop up the error dialog and show the error.
   * It shows the java-error-message and the stack-trace if "details" is pressed.
   * @param message the message which will be shown to the user
   * @param e the exception which has been thrown
   */
  public void showError(String message, Exception e)
  {
    txtMessage.setText(message);
    txtMessage.setCaretPosition(0);
    String stackTrace = "";
    StackTraceElement[] stackElement = e.getStackTrace();
    for(int i=0; i < stackElement.length; i++)
    {
      stackTrace = stackTrace + stackElement[i].toString() + "\n";
    }
    
    txtDetail.setText("java-message\n------------\n" + e.toString() + "\n\nstack-trace\n-----------\n"  + stackTrace);
    txtDetail.setCaretPosition(0);
    
    setVisible(true);
  }
}
